<template>
  <Popup
      :round="round"
      v-model="visible"
      @click-overlay="handleClose"
      :close-on-click-overlay="false"
      :safe-area-inset-bottom="true"
      :transition-appear="true"
      :close-on-popstate="true"
      overlay-class="layer_wrap"
      v-if="BroadcastContent?.visible"
  >
    <div class="room-call-patient">
      <div class="patient-info">
        <div class="current-patient" v-if="BroadcastContent?.treatingAlert">
          <template v-for="(item) in BroadcastContent?.currentContent">
            <div class="item"  v-if="item.type === 'patientInfo'">
              <template v-for="(itemInfo) in item.tempName">
                <div class="item-info">{{itemInfo}}</div>
              </template>
            </div>
            <div class="item" v-html="item.name" v-else/>
          </template>
        </div>
        <div class="wait-patient"
             v-if="waitPatientShown"
             :class="`wait-patient-${BroadcastContent?.waitPatientList?.length}`"
             v-html="BroadcastContent.waitContent">
        </div>
      </div>
    </div>
  </Popup>
</template>

<script>
import {Popup} from 'vant';

export default {
  name: 'RoomCallPatientOriginal',

  componentName: "RoomCallPatientOriginal",

  props: {
    visible: {
      type: Boolean,
      required: false
    },
    info: {
      type: Object,
      required: true
    },
  },

  components: {
    Popup,
  },

  data() {
    return {
      round: true,
    }
  },

  computed: {
    shownPatientInfo() {
      return function (rowList, value) {
        return this.$lodash.some(rowList, function (obj) {
          return obj['type'] === value;
        })
      }
    },

    // 候诊患者是否显示
    waitPatientShown() {
      let {waitingAlert, waitContent, waitPatientList} = (this.BroadcastContent);
      return waitingAlert && !!waitContent && !!(waitPatientList.length)
    },

    // 获取播报的内容
    BroadcastContent() {
      const self = this;
      let msgInfo = Object();
      let {data} = (self.info)
      let msgContent = data?.msgContent || Object();
      if (data?.msgContent?.treatingVoiceContentConfig) {
        // 正在就诊的控制文字
        let treatingVoiceContentConfig = msgContent.treatingVoiceContentConfig;
        // 候诊语音播报的文案
        let waitingVoiceContent = msgContent.waitingVoiceContent;
        if (!!treatingVoiceContentConfig) {
          treatingVoiceContentConfig.forEach((item, index, arr) => {
            let key = item.type;
            let boolean = key === 'orderNo' || key === 'patientName' || key === 'hao' || key === 'treatmentRoomName' || key === 'deptName' || key === 'workerName';
            if (boolean) {
              switch (key) {
                case 'orderNo':
                case 'patientName':
                case 'hao':
                  let patientInfoArr = Array();
                  let temps = new Map();
                  temps.set('orderNo', {key: 'orderNo', state: this.shownPatientInfo(arr, 'orderNo')});
                  temps.set('hao', {key: 'hao', state: this.shownPatientInfo(arr, 'hao')});
                  temps.set('patientName', {key: 'patientName', state: this.shownPatientInfo(arr, 'patientName')});
                  (msgContent.treatingPatientList).forEach((item) => {
                    let patientInfo = '';
                    let orderNoInfo = temps.get('orderNo');
                    let haoInfo = temps.get('hao');
                    let patientNameInfo = temps.get('patientName')
                    if (orderNoInfo.state) {
                      patientInfo = `${self.$lodash.trim(patientInfo)}${self.$lodash.trim(item['orderNo'])}`
                    }
                    if (haoInfo.state) {
                      let numberInfo = this.$lodash.find(arr, { type: 'hao' })
                      let numberName = numberInfo?.name || '号'
                      patientInfo = `${self.$lodash.trim(patientInfo)}${self.$lodash.trim(numberName)}`
                    }
                    if (patientNameInfo.state) {
                      patientInfo = `${self.$lodash.trim(patientInfo)}-${self.$lodash.trim(item['patientName'])}`
                    }
                    patientInfoArr.push(patientInfo)
                  })
                  if(!this.shownPatientInfo(arr, 'patientInfo')){
                    let patientInfoStr = self.$lodash.join(patientInfoArr, '\u3000');
                    arr.splice(index + 1, 0, {type: 'patientInfo', name: patientInfoStr, tempName: patientInfoArr});
                  }
                  break
                case 'treatmentRoomName':
                  Reflect.set(item, 'name', msgContent.treatmentRoomName);
                  break
                case 'deptName':
                  Reflect.set(item, 'name', msgContent.deptName);
                  break
                case 'workerName':
                  Reflect.set(item, 'name', msgContent.workerName);
                  break
              }
            }
          });
        }

        // 判断是否展示全量的患者信息
        let hasPatientAllInfo = this.shownPatientInfo(treatingVoiceContentConfig, 'patientInfo');
        if (hasPatientAllInfo) {
          this.$lodash.remove(treatingVoiceContentConfig, obj => (obj['type'] === 'patientName') || (obj['type'] === 'orderNo') || (obj['type'] === 'hao'))
        }
        //  就诊语音的配置数值信息
        Reflect.set(msgInfo, 'treatingAlert', (msgContent.treatingAlert) || false)
        Reflect.set(msgInfo, 'waitingAlert', (msgContent.waitingAlert) || false)
        Reflect.set(msgInfo, 'visible', !!((msgContent.treatingAlert) || (msgContent.waitingAlert)))
        Reflect.set(msgInfo, 'waitContent', waitingVoiceContent)
        Reflect.set(msgInfo, 'waitPatientList', (msgContent.waitingPatientList) || [])
        Reflect.set(msgInfo, 'currentContent', treatingVoiceContentConfig || [])
        Reflect.set(msgInfo, 'currentPatientInfo', msgContent.treatingPatient)
      }
      return msgInfo;
    },

    //截取当前患者
    patientListInfo: function () {
      let currPatient = this.info['currPatient'];
      if (currPatient.length >= 10) {
        return currPatient.slice(0, 10)
      } else {
        return currPatient
      }
    },
  },

  mounted() {
    console.log('init')
  },

  methods: {
    handleClose() {
      this.$emit('handleCancel');
    }
  },

}
</script>

<style lang="scss" scoped>
@import "./assets/style";
</style>
